package cn.manyufun.myf.common.webview;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import static cn.manyufun.myf.common.webview.BaseWebView.FILECHOOSER_RESULTCODE;

/**
 * Created by Xiao on 2018-02-12.
 */

public class WebServerViewChromeClient extends WebChromeClient {
    private final BaseWebView mBaseWebView;
    private final Activity mCtx;

    public WebServerViewChromeClient(Activity context, BaseWebView baseWebView) {
        this.mCtx = context;
        this.mBaseWebView = baseWebView;
    }

    public void openFileChooser(ValueCallback<Uri> uploadMsg) {

        mBaseWebView.mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        mCtx.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

    }

    // For Android 3.0+
    public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
        mBaseWebView.mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");
        mCtx.startActivityForResult(
                Intent.createChooser(i, "File Browser"),
                FILECHOOSER_RESULTCODE);
    }

    //For Android 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        mBaseWebView.mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        mCtx.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

    }

    // For Android >= 5.0
    @Override
    public boolean onShowFileChooser(WebView webView,
                                     ValueCallback<Uri[]> filePathCallback,
                                     WebChromeClient.FileChooserParams fileChooserParams) {
        mBaseWebView.uploadMessageAboveL = filePathCallback;
        openImageChooserActivity();
        return true;
    }

//    @Override
//    public boolean onCreateWindow(WebView view, boolean isDialog,
//                                  boolean isUserGesture, Message resultMsg) {
//
//        WebView.HitTestResult result = view.getHitTestResult();
//        String data = result.getExtra();
//        CommonUtils.openActivity("normal", data, mCtx);
//        Context context = view.getContext();
//
//        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
//        context.startActivity(browserIntent);
//        return false;
//    }

    private void openImageChooserActivity() {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        mCtx.startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILECHOOSER_RESULTCODE);
    }

}
